define(function(require, exports, module){
	var mycountDetailTemp = require("text!../template/myorder.html");
	
	module.exports = Nuui.View.extend({
		
		template:mycountDetailTemp,
		
		onRender:function(){
			var view = this;
		},
		
		onShow:function(){
			var view=this;
			var condi = {chooses: []};
			init(condi);			
			var CreateTpl=function(obj){
				if(obj){
						var html='';
					
					for(var item=0;item<obj.length;item++){

				html+='';
				html+='		<div class="myorder-list">';
				html+='<div class="list-top">';
				html+='	<span class="top-left">'+obj[item].name+'</span>';
				html+='	<span class="top-right" data-status="'+obj[item].status+'">';
				if(obj[item].status==0){
					html+='待支付';					
				}else if(obj[item].status==1){
					html+='支付成功';
				}else if(obj[item].status==2){
					html+='支付失败';
				}else if(obj[item].status==3){
					html+='异常订单';
				}else if(obj[item].status==4){
					html+='取消订单';
				}
				html+='</span>';
				html+='</div>';
				html+='<div class="list-middle">';
				html+='	<p>订单编号：<span>'+obj[item].numbering+'</span></p>';
				html+='	<p>订单金额：<span><em>'+obj[item].money+'</em>元</span></p>';
				html+='	<p>订单日期：<span>'+obj[item].date+'</span></p>';
				html+='</div>';
				html+='<div class="list-buttom">	';
				html+='	<a href="javascript:void(0)" class="clearOrder">取消订单</a>';
				html+='	<a href="javascript:void(0)" class="toPay">去支付</a>';
				html+='	<div class="img-box paddRight45">';
				html+='		<div class="box-img" href="##">';
				html+='			<img style="width: 100%;height: 100%;" src="images/img/lianxikefu.png" alt="" />';
				html+='		</div>';
				html+='		<div class="box-title">';
				html+='			<p>联系客服</p>';
				html+='		</div>';
				html+='	</div>';
				html+='	<div class="img-box">';
				html+='		<div class="box-img" href="##">';
				html+='			<img style="width: 100%;height: 100%;" src="images/img/quzhifu.png" alt="" />';
				html+='		</div>';
				html+='		<div class="box-title">';
				html+='			<p>投诉</p>';
				html+='		</div>';
				html+='	</div>';
				html+='</div>';
				html+='</div>';

					}
				}
				return html;
				
				
			}
			
			/*筛选*/
            $(".selected").hide();
			$(".select_list li").on("tap",function () {
				$(this).addClass("active").siblings().removeClass("active");
				$(this).find('em').addClass('active');
                var i = $(this).index();
                $('.select-list').eq(i).show().siblings().hide();
                $(".selected").show();
            });
			$('.select-list').on("tap","p",function () {
					$(this).addClass("active").siblings().removeClass("active");
					$(this).find('em');
	                var i=$(this).closest('div').attr('data-index');
					var text = $(this).text().trim();
					var code=$(this).attr('data-code');
	                $('.select_list li').eq(i).attr('data-checked',code).find('a').text(text);
	                $('.select_list li').eq(i).find('em').removeClass('active');
	                $(".selected").hide();
	                /*获取筛选条件*/
	                var type=$('.select_list .type').attr('data-checked')||'all';
	                var date=$('.select_list .date').attr('data-checked')||'all';
	                var status=$('.select_list .status').attr('data-checked')||'all';
	                var choose=[];
	                if(type!=='all'){choose.push({type:'type',value:type})}
	                if(date!=='all'){choose.push({type:'date',value:date})}
	                if(status!=='all'){choose.push({type:'status',value:status})}
	                var conditions = {				
						chooses: choose
					}
					init(conditions)
	                
	            });
			$(".black").on("tap",function () {
				$(this).parent().hide()
			});

			/*取消订单*/
            $(".myorder-section").on("tap",".clearOrder",function () {
                var li=$(this).closest('.myorder-list');
                li.addClass('active').delay(300).queue(function(){
                    $(this).remove();
                    $(this).dequeue();
                });
            })
            /*立即购买*/
			view.$('.order-list').on('tap','.myorder-list .toPay',function(){
				App.navigate('example/index/payMethod',true);
				return false;
			})
			
			/*filter*/
			// 筛选器
			var ProductFilters = {
				/**
				 * 区间类型筛选
				 * @param {array<Product>} products
				 * @param {array<{type: String, low: number, high: number}>} ranges
				 */
				rangesFilter: function(products, ranges) {
					if(ranges.length === 0) {
						return products;
					} else {
						/**
						 * 循环多个区间条件，
						 * 每种区间类型应该只有一个，
						 * 比如价格区间不会有1000-2000和4000-6000同时需要的情况
						 */
						for(var range in ranges) {
							// 多个不同类型区间是与逻辑，可以直接赋值给自身
							products = products.filter(function(item) {
								return item[range.type] >= range.low && item[range.type] <= range.high;
							});
						}
						return products;
					}
				},
				/**
				 * 选择类型筛选
				 * @param {array<Product>} products
				 * @param {array<{type: String, value: String}>} chooses
				 */
				choosesFilter: function(products, chooses) {
					var tmpProducts = [];
					if(chooses.length === 0) {
						tmpProducts = products;
					} else {
						
						/*且&&  逻辑*/
						/*需将第一次过滤获取到的数组，再筛选一遍*/
						var index=0;
						/*第一个条件筛选时,是以所有数据为基础*/
						for(var i=0;i<chooses.length;i++) {
							var choice=chooses[i];
							index++;
							if(index==1){
								tmpProducts=products;
							}
							if(choice.type=='date'){
								var time1=new Date();
								var time2=new Date();
								var week=time1.getDate();
								var month=time1.getMonth();
								var currMonth=time2.format('yyyy-mm-dd');
									tmpProducts = tmpProducts.filter(function(item) {
										if(choice.value=='day'){
											var prevMonth=time1.format('yyyy-mm-dd');
											return item[choice.type]==currMonth;
										}else if(choice.value=='week'){
											time1.setDate(week-7);
											var prevMonth=time1.format('yyyy-mm-dd');
											return item[choice.type]<=currMonth&&item[choice.type]>prevMonth;
										}else if(choice.value=='month'){
											time1.setMonth(month-1);
											var prevMonth=time1.format('yyyy-mm-dd');
											return item[choice.type]<=currMonth&&item[choice.type]>prevMonth;

										}
									});
							}else{
								tmpProducts = tmpProducts.filter(function(item) {
									return item[choice.type]==choice.value;
								});
							}
						}
					return tmpProducts;						
					}
				}
			};
			
			function doFilter(products, conditions) {
				if(conditions.chooses.length==0){
					return products;
				}else{
					// 根据条件循环调用筛选器里的方法
					for(key in conditions) {
						// 判断是否有需要的过滤方法
						if(ProductFilters.hasOwnProperty(key + 'Filter') && typeof ProductFilters[key + 'Filter'] === 'function') {
							products = ProductFilters[key + 'Filter'](products, conditions[key]);
						}
					}
					return products;
				}
			}

			function init(Conditions){
				view.$(".order-list").empty();
				App.request(App.server.getServePath(App.path.myOrderAction), {
					success:function(data){
						if(data.ec){
							alert(data.em);
						}else{
							var result = doFilter(data.orderInfo, Conditions);
							var res=	CreateTpl(result);
							view.$(".order-list").html(res);
						}
					}
				})
			}
		}
	});	
});